package edu.geektime.jiangfan.chapter02;

import lombok.extern.slf4j.Slf4j;

import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.LongAdder;

@Slf4j
public class GCLogAnalysis {
    private static Random random = new Random();
    public static void main(String[] args) {
      long startMillis = System.currentTimeMillis();
      long timeoutMillis =  TimeUnit.MINUTES.toMillis(1);
      long endMillis = startMillis + timeoutMillis;
      LongAdder counter = new LongAdder();
      log.info("正在执行 。。。");
      int cacheSize = 2000;
      Object [] cacheGarbage = new Object[cacheSize];
      while (System.currentTimeMillis() < endMillis) {
         Object garbage = generateGarbage(1024*1024);
         counter.increment();
         int randomIndex = random.nextInt(2 * cacheSize);
         if (randomIndex < cacheSize) {
             cacheGarbage[randomIndex] = garbage;
         }
      }
      log.info("执行结束!共生成对象次数:{}" , counter.longValue());
    }
    // 生成对象
    private static Object generateGarbage(int max) {
        int randomSize = random.nextInt(max);
        int type = randomSize % 4;
        Object result = null;
        switch (type) {
            case 0:
                result = new int[randomSize];
                break;
            case 1:
                result = new byte[randomSize];
                break;
            case 2:
                result = new double[randomSize];
                break;
            default:
                StringBuilder builder = new StringBuilder();
                String randomString = "randomString-Anything";
                while (builder.length() < randomSize) {
                    builder.append(randomString);
                    builder.append(max);
                    builder.append(randomSize);
                }
                result = builder.toString();
                break;
        }
        return result;
    }

}
